package com.mes.handler.plan;

import com.basis.utils.HibernateDao;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.base.exception.OBException;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.service.OBDal;

import java.util.List;
import java.util.Map;

/**
 * 销售订单下发B端
 * @author ZT
 */
public class PlanOrderCallHandler extends BaseActionHandler {
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject jsonRep = new JSONObject();
		try {
			JSONObject jsonData = new JSONObject(data);
			String id = jsonData.getString("recordId");

			this.getPlanOrderJobPlanList(id);
			
		} catch (Exception e) {
			OBDal.getInstance().rollbackAndClose();
			logger.error("销售订单下发失败：", e);
			try {
				jsonRep.put("result", false);
				jsonRep.put("msg", "下发失败！");
			} catch (JSONException e1) {
			}
			throw new OBException(e);
		}
		return jsonRep;
	}


	/**
	 * 根据销售订单ID调用存储过程
	 * @param planOrderId
	 * @return
	 */
	private List<Map<String, Object>> getPlanOrderJobPlanList(String planOrderId) {
		String sql = "SELECT mes_sales_order_to_plan('"+planOrderId+"')";
		return HibernateDao.query(sql );
	}
}